package com.example.springinterceptor.config;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2023/5/7 18:35
 */
    //自定义 异常统一处理类

@ControllerAdvice
@ResponseBody
public class MyExHandler  {

    @ExceptionHandler(Exception.class)
    public  Object exceptionAdvice(Exception e){
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("status",-1);
        hashMap.put("msg","总异常 ： " + e.getMessage());
        hashMap.put("data",null);
        return hashMap;
    }

    @ExceptionHandler(NullPointerException.class)
    public  Object NPExceptionAdvice(NullPointerException e){
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("status",-1);
        hashMap.put("msg","空指针异常 ： " + e.getMessage());
        hashMap.put("data",null);
        return hashMap;
    }

}
